package com.example.watertest;
import java.io.IOException;
import java.util.Calendar;
 
import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.particle.BatchedSpriteParticleSystem;
import org.andengine.entity.particle.ParticleSystem;
import org.andengine.entity.particle.emitter.CircleOutlineParticleEmitter;
import org.andengine.entity.particle.emitter.PointParticleEmitter;
import org.andengine.entity.particle.emitter.RectangleOutlineParticleEmitter;
import org.andengine.entity.particle.initializer.AccelerationParticleInitializer;
import org.andengine.entity.particle.initializer.AlphaParticleInitializer;
import org.andengine.entity.particle.initializer.BlendFunctionParticleInitializer;
import org.andengine.entity.particle.initializer.ExpireParticleInitializer;
import org.andengine.entity.particle.initializer.GravityParticleInitializer;
import org.andengine.entity.particle.initializer.RotationParticleInitializer;
import org.andengine.entity.particle.modifier.ScaleParticleModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.background.EntityBackground;
import org.andengine.entity.scene.background.ParallaxBackground;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.AnimatedSprite.IAnimationListener;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.sprite.UncoloredSprite;
import org.andengine.entity.text.Text;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.IModifier;
import org.andengine.entity.Entity;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.CardinalSplineMoveModifier;
import org.andengine.entity.modifier.CardinalSplineMoveModifier.CardinalSplineMoveModifierConfig;
import org.andengine.entity.modifier.ColorModifier;
import org.andengine.entity.modifier.DelayModifier;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierMatcher;
import org.andengine.entity.modifier.JumpModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveByModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.PathModifier.Path;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.input.touch.TouchEvent;

import android.graphics.Color;
import android.opengl.GLES20;
import android.util.Log;
import android.widget.Toast;
 
 
 
public class MainActivityScreens extends SimpleBaseGameActivity implements IOnSceneTouchListener {
 
protected static final IAnimationListener AnimatedSprite = null;
private Camera mCamera;
private static int WIDTH = 800;
private static int HEIGHT= 480;
private Font miFuente;
private BitmapTextureAtlas miAtlas;
private ITextureRegion texturaChar;
private Sprite charSprite, Boton;
private ITiledTextureRegion texturaAnimada, texturaSonido;
private AnimatedSprite spriteAnimado;
private Text miTexto;
private int cuenta = 0;
private Music music;


/* These three Entity objects will represent different screens */
private final Entity mScreenOne = new Entity();
private final Entity mScreenTwo = new Entity();
private final Entity mScreenThree = new Entity();

/* This entity modifier is defined as the 'transition-in' modifier
 * which will move an Entity/screen into the camera-view */
private final ParallelEntityModifier mMoveInModifier = new ParallelEntityModifier(
  new MoveXModifier(3, WIDTH, 0),
  new RotationModifier(3, 0, 360),
  new ScaleModifier(3, 0, 1));

/* This entity modifier is defined as the 'transition-out' modifier
 * which will move an Entity/screen out of the camera-view */
private final ParallelEntityModifier mMoveOutModifier = new ParallelEntityModifier(
  new MoveXModifier(3, 0, -WIDTH),
  new RotationModifier(3, 360, 0),
  new ScaleModifier(3, 1, 0));



@Override
public EngineOptions onCreateEngineOptions() {
 
// Definimos nuestra camara
mCamera = new Camera(0, 0, WIDTH, HEIGHT);
// Ahora declaramos las opciones del motor
EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), mCamera);
//EngineOptions(es full screen?, Cual es la orientacion de la pantalla?, Como actuaremos ante distintas resoluciones?, y la camara)
// impedimos que la pantalla se apague por inactividad
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
// Return the engineOptions object, passing it to the engine
//para activar la musica
engineOptions.getAudioOptions().setNeedsMusic(true);
return engineOptions;
}
 
@Override
protected void onCreateResources() throws IOException {
 
//primero debemos indicar donde estan las imagenes
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
//luego crear el atlas, darle medidas y un tipo de textura.
miAtlas = new BitmapTextureAtlas(getTextureManager(), 800, 800, TextureOptions.BILINEAR);

texturaAnimada = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(miAtlas, this, "boton_menu.jpg", 70, 0, 2, 1);
texturaSonido = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(miAtlas, this, "sonido.jpg", 10, 100, 2, 1);

//y la cargamos
miAtlas.load();
//Aqui hemos creado un atlas de 256 x 256 para cargar una imagen de 108 x 253
//en el punto 0,0 de miAtlas, osea nos sobra espacio






try
{
    music = MusicFactory.createMusicFromAsset(mEngine.getMusicManager(), this,"musica/musica.mp3");
    music.setLooping(true);
}
catch (IOException e)
{
    e.printStackTrace();
}



}

/* This method is used to swap screens in and out of the camera-view */
private void setLayer(IEntity pLayerIn, IEntity pLayerOut) {
  
  /* If the layer being transitioned into the camera-view is invisible,
   * set it to visibile */
  if (!pLayerIn.isVisible()) {
   pLayerIn.setVisible(true);
  }

  /* Global modifiers must be reset after each use */
  mMoveInModifier.reset();
  mMoveOutModifier.reset();

  /* Register the transitional effects to the screens */
  pLayerIn.registerEntityModifier(mMoveInModifier);
  pLayerOut.registerEntityModifier(mMoveOutModifier);
}
 
@Override
protected Scene onCreateScene() {
 
    final Scene escena = new Scene() {
		  /* Variable which will accumulate time passed to
		   * determine when to switch screens */
		  float timeCounter = 0;

		  /* Define the first screen indices to be transitioned in and out */
		  int layerInIndex = 0;
		  int layerOutIndex = 3 - 1;
		  
		  
			
		  /* Execute the code below on every update to the mScene object */
		  @Override
		  protected void onManagedUpdate(float pSecondsElapsed) {
			  
		    /* If accumulated time is equal to or greater than 4 seconds */
		    if (timeCounter >= 4) {

		     /* Set screens to be transitioned in and out */
		      //setLayer(escena.getChildByIndex(layerInIndex), escena.getChildByIndex(layerOutIndex));

		      /* Reset the time counter */
		      timeCounter = 0;

		      /* Setup the next screens to be swapped in and out */
		      if (layerInIndex >= 3 - 1) {
		        layerInIndex = 0;
		       layerOutIndex = 3 - 1;
		      } else {
		        layerInIndex++;
		        layerOutIndex = layerInIndex - 1;
		      }

		    }
		    /* Accumulate seconds passed since last update */
		    timeCounter += pSecondsElapsed;
		    super.onManagedUpdate(pSecondsElapsed);
		  }
		};

		/* Attach the layers to the scene.
		 * Their layer index (according to mScene) is relevant to the
		 * order in which they are attached */
		escena.attachChild(mScreenOne); // layer index == 0
		escena.attachChild(mScreenTwo); // layer index == 1
		escena.attachChild(mScreenThree); // layer index == 2
		
		return escena;
}


@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
 
  
  return false;
}
 
}